import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import os from 'node:os';

function getNetworkIp(ver = 4) {
  const osType = os.type(); // 系统类型
  const netInfo = os.networkInterfaces();
  let ip = '';
  if (osType === 'Windows_NT') {
    for (const dev in netInfo) {
      // win7的网络信息中显示为本地连接，win10显示为以太网
      if ((dev === '本地连接' || dev === '以太网') && netInfo[dev]) {
        for (let j = 0; j < netInfo[dev].length; j++) {
          if (
            netInfo[dev] &&
            netInfo[dev][j] &&
            netInfo[dev][j]?.family === `IPv${ver}`
          ) {
            ip = netInfo[dev][j]?.address || 'localhost';
            break;
          }
        }
      }
    }
  } else if (osType === 'Linux') {
    const eth0 = netInfo.eth0 ? netInfo.eth0[0] : { address: 'localhost' };
    ip = (eth0 as os.NetworkInterfaceInfo).address || 'localhost';
  }
  return ip;
}
// https://vite.dev/config/
export default defineConfig({
  plugins: [vue()],
      server: {
        proxy: {
          '/terminal-server': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/terminal-server/, ''),
            // 启动terminal服务地址
            target: `http://${getNetworkIp()}:4000/`,
            ws: true,
          },
          '/ws-server': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/ws-server/, ''),
            // 启动terminal服务地址
            target: `http://${getNetworkIp()}:4000/`,
            ws: true,
          },
        },
      },
})
